//----
// PART III - Previously Missing Partners


	/* This file uses Schedule K-1 data with the country
	of the recipient for partners unable to be matched to
	annual returns to describe where net income was flowing
	that was previously "missing". It also 	produces Figure 4. */

	
	use ${data_dir}/missing_partner_table.dta, clear
		
	// exclude income flowing to higher-tier partnerships
	
	drop if class_entity == "P"		
	
	// Drop out of scope (US and partners with missing codes)
	
	drop if country_code=="US"|country_code<"AA"|country_code>"ZZ"
		
		/* Country Codes are IRS Country codes */
		
		/* "" is missing, and ff is foreign but unknown country */
	
	// Sum net income by country
		
	gcollapse (sum) net_income, by(country_code)
		
	replace net_income=net_income/1e9
	
	// Sort
	
	gsort -net_income
	
	// Create mask for table
		
		g order=_n
		g clabel = ""
		replace clabel = "Cayman Isl." if country_code=="CJ"
		replace clabel = "Netherlands" if country_code=="NL"
		replace clabel = "UK" if country_code=="UK"
		replace clabel = "British Virg. Isl." if country_code=="VI"
		replace clabel = "Luxembourg" if country_code=="LU"
		replace clabel = "Canada" if country_code=="CA"
		replace clabel = "Mexico" if country_code=="MX"
		replace clabel = "Bermuda" if country_code=="BD"
		replace clabel = "Singapore" if country_code=="SN"
		replace clabel = "Guernsey" if country_code=="GK"
		replace clabel = "Switzerl." if country_code=="SZ"
		replace clabel = "Jersey" if country_code=="JE"
		replace clabel = "Domin. Rep." if country_code=="DR"
		replace clabel = "Hong Kong" if country_code=="HK"
		replace clabel = "Curacao" if country_code=="UC"
		replace clabel = "Ireland" if country_code=="EI"
		replace clabel = "Germany" if country_code=="GM"
		replace clabel = "Australia" if country_code=="AS"
		replace clabel = "China" if country_code=="CH"
		replace clabel = "Kuwait" if country_code=="KU"

	// Clean
	
		keep net_income order clabel
		
		keep if _n<21
		
	// --------
	// Figure 4
		
		labmask order, values(clabel)
		graph bar net_income, bar(1, color(gray*1.5)) ///
			over(order) horiz blabel(total, format(%5.0f)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(6)) ///
			ylabel(0(50)275,labsize(small)) ///
			leg(off)
			graph export ${fig_dir}/fig_missing_countries.png, replace width(1000)
		

		labmask order, values(clabel)
		graph bar net_income, bar(1, color(navy)) ///
			over(order) horiz blabel(total, format(%5.0f)) ///
			plotregion(lcolor(black)) ///
			graphregion(fcolor(white) lcolor(white)) ///
			bgcolor(white) ///
			ytitle("Billions of 2020 Dollars", height(6)) ///
			ylabel(0(50)275,labsize(small)) ///
			leg(off)
			graph export ${fig_dir}/fig_missing_countries_color.png, replace width(1000)
		
	// Export

		foreach v of varlist net_income {
			replace `v'= round(`v',.1)
			}
	
		export delimited using ${out_dir}/fig4_tab.csv, replace
			
	
